//=============================================================================
// GameCheatManager
// Object within gameplayercontroller that manages "cheat" commands
// only spawned in single player mode
// Copyright 1998-2011 Epic Games, Inc. All Rights Reserved.
//=============================================================================

class GameCheatManager extends CheatManager within GamePlayerController
	config(Game)
	native;

/** Debug camera - used to have independent camera without stopping gameplay */
var DebugCameraController			DebugCameraControllerRef;
var class<DebugCameraController>	DebugCameraControllerClass;
var config string					DebugCameraControllerClassName;


function PatchDebugCameraController()
{
	local class<DebugCameraController> TempCameraControllerClass;

	if (DebugCameraControllerClassName != "")
	{
		TempCameraControllerClass = class<DebugCameraController>(DynamicLoadObject(DebugCameraControllerClassName, class'Class'));

		if (TempCameraControllerClass != None)
		{
			DebugCameraControllerClass = TempCameraControllerClass;
		}
	}
}

/**
 * Toggle between debug camera/player camera without locking gameplay and with locking
 * local player controller input.
 */
exec function ToggleDebugCamera(optional bool bDrawDebugText = true)
{
	local PlayerController PC;
	local DebugCameraController DCC;

	foreach WorldInfo.AllControllers(class'PlayerController', PC)
	{
		if ( PC.bIsPlayer && PC.IsLocalPlayerController() )
		{
			DCC = DebugCameraController(PC);
			if( DCC!=none && DCC.OriginalControllerRef==none )
			{
				//dcc are disabled, so we are looking for normal player controller
				continue;
			}
			break;
		}
	}

	if( DCC!=none && DCC.OriginalControllerRef!=none )
	{
		DCC.DisableDebugCamera();
		DCC.Destroy();
		DCC = None;
	}
	else if( PC!=none )
	{
		EnableDebugCamera(bDrawDebugText);
	}
}

/**
 *  Switch controller to debug camera without locking gameplay and with locking
 *  local player controller input
 */
function EnableDebugCamera(bool bEnableDebugText)
{
	local Player P;
	local vector eyeLoc;
	local rotator eyeRot;
	local float CameraFOVAngle;

	P = Player;
	if( P!= none && Pawn != none && IsLocalPlayerController() )
	{
		PatchDebugCameraController();
		if( DebugCameraControllerRef!=None )
		{
			DebugCameraControllerRef.Destroy();
		}

		CameraFOVAngle = GetFOVAngle();

		DebugCameraControllerRef = Spawn(DebugCameraControllerClass);
		DebugCameraControllerRef.PlayerInput = none;
		DebugCameraControllerRef.OriginalPlayer = P;
		DebugCameraControllerRef.OriginalControllerRef = outer;

		GetPlayerViewPoint(eyeLoc,eyeRot);
		DebugCameraControllerRef.SetLocation(eyeLoc);
		DebugCameraControllerRef.SetRotation(eyeRot);
		DebugCameraControllerRef.bDrawDebugText=bEnableDebugText;

		P.SwitchController( DebugCameraControllerRef );
		DebugCameraControllerRef.OnActivate( outer );

		// Make sure the camera gets created and set it up.
		DebugCameraControllerRef.GetPlayerViewPoint(eyeLoc,eyeRot);
		if ( DebugCameraControllerRef.PlayerCamera != None )
		{
			DebugCameraControllerRef.PlayerCamera.SetFOV( CameraFOVAngle );
			DebugCameraControllerRef.PlayerCamera.UpdateCamera(0.0);
		}
		else
		{
			DebugCameraControllerRef.FOVAngle = CameraFOVAngle;
		}
	}
}

/**
 ** Simple function to illustrate the use of the HttpRequest system.
 **/
exec function TestHttp()
{
	local HttpRequestInterface R;

	`log("Creating request");

	// create the request instance using the factory (which handles
	// determining the proper type to create based on config.
	R = class'HttpFactory'.static.CreateRequest();
	// always set a delegate instance to handle the response.
	R.SetRequestCompleteDelegate(OnRequestComplete);
	`log("Created request");
	// you can make many requests from one request object.
	R.SetURL("http://www.google.com");
	R.ProcessRequest();
	R.SetURL("http://www.epicgames.com");
	R.ProcessRequest();
	R.SetURL("http://www.microsoft.com");
	R.ProcessRequest();
	R.SetURL("http://DoesNotExistAtAllAAAAAA");
	R.ProcessRequest();
	R.SetURL("http://localhost/PWS/PostProfileHandler.1?Key1=Value1&Key2=value3&Key3NoValue&Key4NoValue&Key5=Value5&Key6NoValue=&Key7NoValue=");
	R.SetVerb("POST");
	// there is currently no way to distinguish keys that are empty from keys that aren't there.
	`log("Key1 = "@R.GetURLParameter("Key1"));
	`log("Key2 = "@R.GetURLParameter("Key2"));
	`log("Key3NoValue = "@R.GetURLParameter("Key3NoValue"));
	`log("NonexistentKey = "@R.GetURLParameter("NonexistentKey"));
	// A header will not necessarily be present if you don't set one. Platform implementations
	// may add things like Content-Length when you send the request, but won't necessarily
	// be available in the Header.
	R.ProcessRequest();
	`log("Request sent");
}

/** Delegate to use for HttpResponses. */
function OnRequestComplete(HttpResponseInterface Response, bool bSucceeded)
{
	local array<String> Headers;
	local String Header;

	`log("Got response!!!!!!! Succeeded="@bSucceeded);
	`log("URL="@Response.GetURL());
	// if we didn't succeed, we can't really trust the payload, so you should always really check this.
	if (bSucceeded)
	{
		Headers = Response.GetHeaders();
		foreach Headers(Header)
		{
			`log("Header:"@Header);
		}
		// GetContentAsString will make a copy of the payload to add the NULL terminator,
		// then copy it again to convert it to TCHAR, so this could be fairly inefficient.
		// This call also assumes the payload is UTF8 right now, as truly determining the encoding
		// is content-type dependent.
		// You also can't trust the content-length as you don't always get one. You should instead
		// always trust the length of the content payload you receive.
		`log("Payload:"@Response.GetContentAsString());
	}
}

defaultproperties
{
}
